<?php

namespace Home\Controller;

use Think\Controller;
use Home\Model;

class OrdersController extends Controller {

    function _initialize() {
        if (empty($_SESSION['account_user_id'])) {
            redirect("/Home/Login/index");
        }
    }

    public function index() {
        if (!IS_POST) {
            $this->display();
            die;
        }
        $account_user_id = $_SESSION['account_user_id'];
        $page = empty($_POST['order_page']) ? 1 : $_POST['order_page'];
        $date = $_POST['time'];
        if ($date == 1) {
            $date = date("Ymd", time() - 3600 * 24);
        } else if ($date == 3) {
            $date = date("Ymd", time() + 3600 * 24);
        } else {
            $date = date("Ymd");
        }
        $startDT = $date . "000000";
        $endDT = $date . "235959";
        $where['pickuptime'] = array('between', array($startDT, $endDT));
        $where['shopaccountid'] = empty($account_user_id) ? 1 : $account_user_id;
        $type = empty($_POST['type']) ? 1 : $_POST['type'];
        if ($type == 1) {
            $where['guserstatus'] = array('in', '0,1,2,3');
        } else {
            $where['guserstatus'] = array('in', '4,5');
        }
        $where['gorderstatus'] = 1;

        $orderModel = new \Home\Model\OrdersModel();
        $result = $orderModel->getList($where, $page);
        foreach ($result as $key => &$val) {
            $val['realpayprice'] = $val['realpayprice'] / 100;
        }
        $list = $this->dataGroup($result, 'order_no');
        if (!empty($list)) {
            $this->ajaxReturn(array('code' => 10000, 'msg' => '请求成功', 'data' => $list));
        } else {
            $this->ajaxReturn(array('code' => 10001, 'msg' => '暂无数据', 'data' => array()));
        }
    }

    public function tabDoor() {
        $orderId = empty($_POST['order_no']) ? "R1907022127436302" : $_POST['order_no'];
        $account_user_id = $_SESSION['account_user_id'];
        $shopAccountModel = new \Admin\Model\ShopAccountModel();
        $shopAccount = $shopAccountModel->getFind(array('id' => $account_user_id));
        $token = $shopAccount['token'];
        $url = "https://www.mymamae.com/device/ScanQRCode";
        $post_data = array('OrderId' => $orderId, 'C_B' => "D");
        $post_data = json_encode($post_data);
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_HEADER, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json", "Token:" . $token));
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
//        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, '0');
        $result = curl_exec($curl);
        curl_close($curl);
        $result = strstr($result, '{"Code');
        $result = json_decode($result,true);
        $this->ajaxReturn(array('code'=>$result['Code'],'msg'=>$result['Message']));
    }
    
    public function getDoors(){
        $account_user_id = $_SESSION['account_user_id'];
        $gInfoModel = new \Home\Model\gInfoModel();
        $where['shopaccountid'] = $account_user_id;
        $page = empty($_POST['page']) ? 1 : $_POST['page'];
        $result = $gInfoModel->getList($where, $page);
        foreach($result as $key => &$val){
            if(empty($val['nickname'])){
                unset($val);
            }
        }
        if($result){
            $this->ajaxReturn(array('code'=>10000,'msg'=>'请求成功','data'=>$result));
        }else{
            $this->ajaxReturn(array('code'=>10001,'msg'=>'暂无数据','data'=>array()));
        }
    }
    
    public function addTasksData(){
        $account_user_id = $_SESSION['account_user_id'];
        $id = $_POST['id'];
        $gdoorModel = new \Home\Model\gDoorModel();
        $door = $gdoorModel->getFind(array('id'=>$id));
        $data['createdt'] = date("Y-m-d H:i:s");
        $data['X'] = $door['codex'];
        $data['Y'] = $door['codey'];
        $data['CMDType'] = "OpenDoor";
        $data['ExpireDT'] = date("Y-m-d H:i:s",time()+120);
        $data['shopaccountid'] = $account_user_id;
        $data['orderid'] = 0;
        $data['isexcuted'] = false;
        $tasksModel = new \Home\Model\TasksModel();
        $result = $tasksModel->addData($data);
        if($result){
            $this->ajaxReturn(array('code'=>10000,'msg'=>'请求成功','data'=>array()));
        }else{
            $this->ajaxReturn(array('code'=>10001,'msg'=>'请求错误','data'=>array()));
        }
    }
    
    public function guizi(){
        $this->display();
    }
    
    protected function dataGroup($dataArr, $keyStr) {
        $newArr = [];
        foreach ($dataArr as $k => $val) {
            $newArr[$val[$keyStr]][] = $val;
        }
        return $newArr;
    }

    public function test() {
        dump($_SESSION);
    }

}
